package cn.dinkevin.xui.util;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import cn.dinkevin.xui.XUI;

/**
 * 网络相关工具类
 * @author chengpengfei
 */
public final class NetworkUtil {

	private NetworkUtil(){}

	/**
	 * 判断当前网络是否是移动网络
	 * @return boolean
	 */
	public static boolean is3G() {
		Context context = XUI.getInstance().getContext();
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
			return true;
		}
		return false;
	}

	/**
	 * 判断当前网络是否是wifi网络
	 * @return boolean
	 */
	public static boolean isWifi() {
		Context context = XUI.getInstance().getContext();
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		}
		return false;
	}

	/**
	 * 判断当前网络是否是2G网络
	 * @return boolean
	 */
	public static boolean is2G() {
		Context context = XUI.getInstance().getContext();
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
				|| activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS || activeNetInfo
				.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
			return true;
		}
		return false;
	}

	/**
	 * WI-FI是否打开
	 * @return
     */
	public static boolean isWifiEnabled() {
		Context context = XUI.getInstance().getContext();
		ConnectivityManager mgrConn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		TelephonyManager mgrTel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
				.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
				.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
	}

	/**
	 * 判断是否有网络连接
	 * @return
	 */
	public static boolean isNetworkConnected() {
		Context context = XUI.getInstance().getContext();
		if (context != null) {
			// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			// 获取NetworkInfo对象
			NetworkInfo networkInfo = manager.getActiveNetworkInfo();
			//判断NetworkInfo对象是否为空
			if (networkInfo != null)
				return networkInfo.isAvailable();
		}
		return false;
	}

	/**
	 * 判断 MOBILE 网络是否可用
	 * @return
	 */
	public static boolean isMobileConnected() {
		Context context = XUI.getInstance().getContext();
		if (context != null) {
			//获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			//获取NetworkInfo对象
			NetworkInfo networkInfo = manager.getActiveNetworkInfo();
			//判断NetworkInfo对象是否为空 并且类型是否为MOBILE
			if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
				return networkInfo.isAvailable();
		}
		return false;
	}

	/**
	 * 获取当前网络连接的类型信息
	 * @return
	 */
	public static int getConnectedType() {
		Context context = XUI.getInstance().getContext();
		if (context != null) {
			//获取手机所有连接管理对象
			ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context
					.CONNECTIVITY_SERVICE);
			//获取NetworkInfo对象
			NetworkInfo networkInfo = manager.getActiveNetworkInfo();
			if (networkInfo != null && networkInfo.isAvailable()) {
				//返回NetworkInfo的类型
				return networkInfo.getType();
			}
		}
		return -1;
	}

	/**
	 * 获取当前的网络状态 ：没有网络-0：WIFI网络1：4G网络-4：3G网络-3：2G网络-2
	 * 自定义
	 * @return
	 */
	public static int getAPNType() {
		Context context = XUI.getInstance().getContext();
		//结果返回值
		int netType = 0;
		//获取手机所有连接管理对象
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		//获取NetworkInfo对象
		NetworkInfo networkInfo = manager.getActiveNetworkInfo();
		//NetworkInfo对象为空 则代表没有网络
		if (networkInfo == null) {
			return netType;
		}
		//否则 NetworkInfo对象不为空 则获取该networkInfo的类型
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_WIFI) {
			//WIFI
			netType = 1;
		} else if (nType == ConnectivityManager.TYPE_MOBILE) {
			int nSubType = networkInfo.getSubtype();
			TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService
					(Context.TELEPHONY_SERVICE);
			//3G 联通的3G为 UMTS 或 HSDPA 电信的3G为 EVDO
			if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
					&& !telephonyManager.isNetworkRoaming()) {
				netType = 4;
			} else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
					|| nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
					|| nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
					&& !telephonyManager.isNetworkRoaming()) {
				netType = 3;
				//2G 移动和联通的2G为GPRS或EGDE，电信的2G为CDMA
			} else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
					|| nSubType == TelephonyManager.NETWORK_TYPE_EDGE
					|| nSubType == TelephonyManager.NETWORK_TYPE_CDMA
					&& !telephonyManager.isNetworkRoaming()) {
				netType = 2;
			} else {
				netType = 2;
			}
		}
		return netType;
	}

	/**
	 * 判断GPS是否打开
	 * ACCESS_FINE_LOCATION权限
	 * @return
	 */
	public static boolean isGPSEnabled() {
		Context context = XUI.getInstance().getContext();
		//获取手机所有连接LOCATION_SERVICE对象
		LocationManager locationManager = ((LocationManager) context.getSystemService(Context
				.LOCATION_SERVICE));
		return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}

	/**
	 * 获得本机ip地址
	 * @return
	 */
	public static String getHostIp() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements(); ) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
						.hasMoreElements(); ) {
					InetAddress inetAddress = ipAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress();
					}
				}
			}
		} catch (SocketException ex) {
		} catch (Exception e) {
		}
		return null;
	}
	
	/**
	 * 查网络连接状态
	 * @return true -> 当前网络可用；否则表示不可用
	 */
	public static boolean isNetworkAvailable() {
        try {
            Context context = XUI.getInstance().getContext();
            if (null == context) {
                return false;
            }
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();
            if (null != info && info.isConnected() && info.isAvailable()) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
	}

}
